package com.yunxin.customui.service.oldtable;

import cn.hutool.core.map.CaseInsensitiveMap;
import com.yunxin.customui.bean.table.WebTableBean;
import com.yunxin.customui.bean.table.sqltable.TableSqlColumn;
import com.yunxin.customui.bean.table.sqltable.TableSqlConfig;
import com.yunxin.customui.mapper.ExecuteMapper;
import com.yunxin.customui.service.dbservice.ConfigService;
import com.yunxin.customui.util.CustomSQLUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 表格数据获取业务逻辑层
 */
@Service
public class TableDataUIService {

    @Resource
    ExecuteMapper executeMapper;

    @Autowired
    ConfigService configService;



    /**
     * 获取表格数据并返回
     * @param tableId 表格id
     * @param param 参数集合（后面可能需要后台再封装）
     * @return
     * @throws Exception
     */
    public WebTableBean getTableDatas(int tableId, Map<String,Object> param) {

        List<CaseInsensitiveMap<String, Object>> tableData = getTableData(tableId, param);

        List<TableSqlColumn> tableColumns = configService.queryTableSqlColumnById(tableId);
        WebTableBean webTableBean = new WebTableBean();
        webTableBean.setDatas(tableData);
        webTableBean.setColumns(tableColumns);
        return webTableBean;
    }

    public List<CaseInsensitiveMap<String, Object>> getTableData(Integer tableId, Map<String,Object> param){
        TableSqlConfig tableSqlConfig = configService.queryTableSqlConfigById(tableId);
        String sql = CustomSQLUtil.buildSQL(tableSqlConfig.getTableSql(), param);
        System.out.println("执行SQL："+sql);
        return executeMapper.executeReadSql(sql);
    }




}
